package qeb.handheldmedical;

import java.text.BreakIterator;

import qeb.handheldmedical.R.color;
import qeb.handheldmedical.R.drawable;
import qeb.handheldmedical.approval.ApprovalMain;
import qeb.handheldmedical.approval.ApprovalOperationPatients;
import qeb.handheldmedical.exam.LineExamMain;
import qeb.handheldmedical.helper.MaterApp;
import qeb.handheldmedical.model.User;
import qeb.handheldmedical.patient.InPatientList;
import qeb.handheldmedical.patient.PatientMain;
import qeb.handheldmedical.user.UserInfo;
import android.R.string;
import android.app.Activity;
import android.app.ActivityGroup;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.ActivityInfo;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.view.GestureDetector;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.ScrollView;
import android.widget.TextView;

/**
 * @author 作者(dugaoyi) E-mail:dugaoyi@163.com
 * @version 创建时间：2016年10月22日 下午3:59:24
 * @declaration
 */
public class SystemMain extends ActivityGroup {
	public Activity first = new InPatientList();
	public Activity second = new ApprovalMain();
	public Activity third = new LineExamMain();
	private User usr = new User();
	private String isApply = "0";
	private static SystemMain mInstance;
	private Intent intent = null;
	public static ScrollView container = null;
	private MaterApp meterApp;
	private ImageView btnModule1, btnModule2, btnModule3, btnModule4;
	private TextView tvModule1, tvModule2, tvModule3, tvModule4;
	MyBroadcast broadcastReceiver = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);

		usr = (User) getIntent().getSerializableExtra("userinfo");
		isApply = String.valueOf(getIntent().getSerializableExtra("isApply"));
		isApply = isApply.equals("1") ? "1" : "0";
		// 隐藏标题栏
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		// 设置视图
		setContentView(R.layout.system_main);
		container = (ScrollView) findViewById(R.id.containerBody);

		meterApp = MaterApp.getInstance();
		// 模块1
		btnModule1 = (ImageView) findViewById(R.id.btnModule1);
		// 模块2
		btnModule2 = (ImageView) findViewById(R.id.btnModule2);
		// 模块3
		btnModule3 = (ImageView) findViewById(R.id.btnModule3);
		// 模块3
		btnModule4 = (ImageView) findViewById(R.id.btnModule4);

		tvModule1 = (TextView) findViewById(R.id.tvModule1);
		tvModule2 = (TextView) findViewById(R.id.tvModule2);
		tvModule3 = (TextView) findViewById(R.id.tvModule3);
		tvModule4 = (TextView) findViewById(R.id.tvModule4);

		btnModule1.setOnClickListener(MyOnClickListener);
		btnModule2.setOnClickListener(MyOnClickListener);
		btnModule3.setOnClickListener(MyOnClickListener);
		btnModule4.setOnClickListener(MyOnClickListener);
		btnModule1.callOnClick();

		broadcastReceiver = new MyBroadcast();
		IntentFilter filter = new IntentFilter("com.handheldMedical.msg");
		// 注册广播接收器
		registerReceiver(broadcastReceiver, filter);
	}

	private OnClickListener MyOnClickListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			Intent intent = new Intent();
			btnModule1.setImageResource(drawable.tb_br);
			btnModule2.setImageResource(drawable.tb_sp);
			btnModule3.setImageResource(drawable.toolbar_zs);
			btnModule4.setImageResource(drawable.tb_ys);
			tvModule1.setTextColor(color.bl);
			tvModule2.setTextColor(color.bl);
			tvModule3.setTextColor(color.bl);
			tvModule4.setTextColor(color.bl);

			switch (v.getId()) {
			case R.id.btnModule1:
				btnModule1.setImageResource(drawable.tb_br1);
				tvModule1.setTextColor(Color.parseColor("#064a5e"));
				intent = new Intent(SystemMain.this, InPatientList.class);
				intent.putExtra("userinfo", usr);
				break;
			case R.id.btnModule2:
				btnModule2.setImageResource(drawable.tb_sp1);
				tvModule2.setTextColor(Color.parseColor("#064a5e"));
				intent = new Intent(SystemMain.this, ApprovalMain.class);
				intent.putExtra("userinfo", usr);
				break;
			case R.id.btnModule3:
				btnModule3.setImageResource(drawable.toolbar_zs1);
				tvModule3.setTextColor(Color.parseColor("#064a5e"));
				intent = new Intent(SystemMain.this, LineExamMain.class);
				intent.putExtra("userinfo", usr);
				break;
			case R.id.btnModule4:
				btnModule4.setImageResource(drawable.tb_ys1);
				tvModule4.setTextColor(Color.parseColor("#064a5e"));
				intent = new Intent(SystemMain.this, UserInfo.class);
				intent.putExtra("userinfo", usr);
				swicthActivity(intent);
				break;
			default:
				break;
			}

			swicthActivity(intent);

		}
	};

	public void swicthActivity(Intent intent) {
		container.removeAllViews();
		intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
		container.addView(getLocalActivityManager().startActivity("frame",
				intent).getDecorView());

	}

	public class MyBroadcast extends BroadcastReceiver {

		@Override
		public void onReceive(Context context, Intent intent) {
			// TODO 自动生成的方法存根
			usr = (User) intent.getSerializableExtra("userinfo");
			btnModule1.callOnClick();
		}

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.app.ActivityGroup#onDestroy()
	 */
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		unregisterReceiver(broadcastReceiver);
		super.onDestroy();
	}

	/* 返回按键事件 */
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			final Intent intent_back = new Intent(SystemMain.this,
					Welcome.class);
			intent_back.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
			SystemMain.this.getApplicationContext().startActivity(intent_back);
			SystemMain.this.finish();
		}
		return false;
	}
}
